//no where ye:
//File->Open Next->macro 

//////////////////////////////////////////////////////////////////////////////////////////////////////////
// takes an image, user selects rectangle tangent 4x to circular roi of plate
// adjust threshold method, adjust min/max object size 
// adjust rolling ball radius
// adjust which files are saved?
// schema for directory structure?
// make the python/cv shit into an imagej plugin?
// current opencv plugins only implement single library functions!
///////////////////////////////////////////////////////////////
function waitForKey(key){
while(!isKeyDown(key)){
//this is shitty and there should be a keyboard interrupt
wait(20);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
macro "SegmentWormsNaive Action Tool - R00cO11cc [f1]" {

//File.* is of the last opened (??? maybe selected) file
fh = File.name;
fh_bare = File.nameWithoutExtension;
dir = File.directory;
outputdir = File.directory + "platephotos/"
File.makeDirectory(outputdir);
//unnecessary if this is being looped over a directory
//open(dir+fh);

selectWindow(fh);
// bounding rect format: (bx,by,bdx,bdy) beginning at the top left corner
// change to user input
//makeRectangle(676,208,1948,1956);
//MUST be a rectangle for crop or else corners are zeroed out and threshold sux
getSelectionBounds(bx,by,bdx,bdy)

run("Crop");
saveAs("PNG", outputdir+fh_bare+"-crop.png");
run("Subtract Background...", "rolling=20");
saveAs("PNG", outputdir+fh_bare+"-crop-bkg.png");
//getList("threshold.methods") 
//the space in " dark" is important
//the dark in space is important

method = "Huang"+" dark"; setAutoThreshold(method);
//setThreshold(8, 255); //just for the arguments

run("Make Binary", "thresholded remaining");
run("Make Binary"); //not sure if this is necessary...

//replace mee with user input OR centered on rectangle of window
// ^^ done?
//getDimensions(width, height, channels, slices, frames)
makeOval(0, 0, bdx, bdy);
run("Analyze Particles...", "size=6-500 circularity=0.00-1.00 show=[Overlay Outlines] display clear");
selectWindow("Results");
// it's actually a FUCKING TSV but imagej refuses to give it that extension
saveAs("text",outputdir+fh_bare+"-particles.tsv");
run("Close"); //should be a close file op?
//selectWindow(fh); //maybe is se;ect by default? todo robutify tho
saveAs("PNG", outputdir+fh_bare+"-particles.png");
run("Close") //should be a close file op?
//dump() //it's never too late to take a dump!
} //macro's over
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// this is for the origin/vehicle/butanone spots
// runs on cropped images, saves coords of three point selections
//////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////
macro "LocateSpots Action Tool - C0a0L18f8L818f [f2]" {

//File.* is of the last opened (??? maybe selected) file
fh = File.name;
fh_bare = File.nameWithoutExtension;
dir = File.directory;
//unnecessary if this is being looped over a directory
//open(dir+fh);

//directories for output, just to make 'batch' easier;
coordsoutputdir = "spots/";

//wait for selection dots to be put into position
//waitForUser("click OK when done...");
//if NOT doing with "batch mode" don't hang
if( is("Batch Mode") ){
waitForKey("space");
}

// xcoords, ycoords are arrays
// this needs a multipoint selection
getSelectionCoordinates(xcoords,ycoords);

//todo: if exists wrapper here
File.makeDirectory(dir+coordsoutputdir);
coordsfh = File.open(dir+coordsoutputdir+fh_bare+".spots");

//Array.print() is FUCKED
for(i=0; i<xcoords.length;i++) {
//File.append(toString(xcoords[i]) + " " + toString(ycoords[i]), dir+coordsoutputdir+fh+"-spots"l )
buffer = toString(xcoords[i]) + " " + toString(ycoords[i]);
print(coordsfh,buffer);
//File.append(buffer,coordsfh);
}

File.close(coordsfh);

} //macro's over
